﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace NowFloats.RichSnippetExtractor.MicroData
{
    public class MicroDataRequest
    {
        #region Micro Data Request Basic Operation
        
        //public MicroDataItem GetMicroData(String Url)
        //{
        //    HtmlDocument document = (new HtmlWeb()).Load(Url);
        //    return GetMicroData(document);
        //}

        //public MicroDataItem GetMicroData(HtmlDocument document)
        //{
        //    return GetMicroData(document.DocumentNode);
        //}

        //public MicroDataItem GetMicroData(HtmlNode node)
        //{
        //    var item = CreateMicroDataItemFromHtmlNode(node);
        //    return item;
        //}

        //private MicroDataItem CreateMicroDataItemFromHtmlNode(HtmlNode node)
        //{
        //    MicroDataItem item = null;
        //    if (node.Attributes.Contains("itemscope"))
        //    {
        //        item = new MicroDataItem();
        //        if (node.Attributes.Contains("itemtype"))
        //            item.Type = node.Attributes["itemtype"].Value;
        //    }
        //    foreach (var childNode in node.ChildNodes)
        //    {
        //        if (childNode.Attributes.Contains("itemscope"))
        //        {
        //            var tempItem = CreateMicroDataItemFromHtmlNode(childNode);
        //            if (tempItem != null && item != null)
        //                item.Items.Add(tempItem);
        //        }
        //        else if (childNode.Attributes.Contains("itemprop"))
        //        {
        //            MicroDataProperty property = new MicroDataProperty()
        //            {
        //                Property = childNode.Attributes["itemprop"].Value
        //            };
        //            if (item != null)
        //            {
        //                if (item.Properties == null)
        //                    item.Properties = new List<MicroDataProperty>();
        //                item.Properties.Add(property);
        //            }
        //        }
        //        else
        //        {
        //            if (childNode.ChildNodes.Count > 0)
        //            {
        //                var tempItem = CreateMicroDataItemFromHtmlNode(childNode);
        //                if (tempItem != null)
        //                    item = tempItem;
        //            }
        //        }
        //    }
        //    return item;
        //}
        #endregion

        public float? GetRating(String Url)
        {
            HtmlDocument document = (new HtmlWeb()).Load(Url);
            return GetRating(document);
        }

        public float? GetRating(HtmlDocument document)
        {
            foreach (var node in document.DocumentNode.Descendants())
            {
                if (node.Attributes.Contains("itemtype"))
                {
                    if (String.Compare(node.Attributes["itemtype"].Value, "http://data-vocabulary.org/Review", StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        foreach (var childNode in node.Descendants())
                        {
                            if (childNode.Attributes.Contains("itemprop"))
                            {
                                if (String.Compare(childNode.Attributes["itemprop"].Value, "rating", StringComparison.InvariantCultureIgnoreCase) == 0)
                                {
                                    float rating;
                                    if (float.TryParse(childNode.InnerText, out rating))
                                        return rating;
                                }
                            }
                        }
                    }
                }
            }
            return null;
        }

        public float? GetAggregateRating(String Url)
        {
            HtmlDocument document = (new HtmlWeb()).Load(Url);
            return GetAggregateRating(document);
        }

        public float? GetAggregateRating(HtmlDocument document)
        {
            HtmlNode ratingNode = null; 
            foreach (var node in document.DocumentNode.Descendants())
            {
                if (node.Attributes.Contains("itemprop"))
                {
                    if (String.Compare(node.Attributes["itemprop"].Value, "aggregateRating", StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        ratingNode = node;
                        break;
                    }
                }
            }
            if (ratingNode != null)
            {
                foreach (var node in ratingNode.Descendants())
                {
                    if (node.Attributes.Contains("itemprop"))
                    {
                        if (String.Compare(node.Attributes["itemprop"].Value, "ratingValue", StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            float result;
                            var rating = node.InnerText;
                            if (float.TryParse(rating, out result))
                            {
                                return result;
                            }
                        }
                    }
                }
            }
            return null;
        }

    }
}

